<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>系统消息</span>
    </div>
    <div class="line"></div>
    <message-modal></message-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import MessageModal from '_c/message-modal'
import TablePage from '_c/table-page'
export default {
  components: {
    MessageModal,
    TablePage
  },
  data () {
    return {
      page: 1,
      rows: 20,
      title: '系统管理系统消息',
      index: '',
      confirmData: {},
      loading: true,
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '时间',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '标题',
          key: 'title',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.title ? params.row.title : '-')
          }
        },
        {
          title: '摘要',
          key: 'content',
          minWidth: 300,
          align: 'center'
        },
        {
          title: '状态',
          key: 'state',
          align: 'center',
          minWidth: 120,
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.state === 0 ? '未读' : params.row.state === 1 ? '已读' : '-'
            )
          }
        },
        {
          title: '操作',
          key: 'cz',
          minWsidth: 120,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row.id, params.row)
                  }
                }
              }, '查看')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      messageList: state => state.message.messageList,
      dataCount: state => state.message.messageDataCount,
      historyData: state => state.message.messageHistoryData,
      pageSize: state => state.message.messagePageSize
    })
  },
  methods: {
    ...mapActions([
      'getMessageData',
      'getMessageCurrentRow',
      'editMessageData',
      'updateModalState',
      'updateDeleteModalState',
      'changeMessagePage',
      'changeMessagePageSize'
    ]),
    handleClick (id, row) {
      // console.log(row)
      let newRow = {}
      this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getMessageCurrentRow(newRow)
      this.editMessageData({ id: row.id }).then(
        setTimeout(() => {
          this.getMessageData({
            page: this.page,
            rows: this.rows
          })
        }, 100)
      )
      this.updateModalState(true)
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeMessagePage(index)
      this.getMessageData({
        page: index,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeMessagePageSize(value)
      this.changePage(this.page)
      this.getMessageData({
        rows: value
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getMessageData({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
